package spectra.ee.web.notice.service;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import spectra.base.log.Logger;
import spectra.base.sql.ibatis.SqlMapperWeb;
import spectra.base.util.DateUtil;
import spectra.ee.web.notice.model.Notice;
import spectra.ee.web.notice.model.NoticeReader;

/**
 * 공지사항 관련 클래스.
 *
 * @author kmhan
 */
public class NoticeServiceBase
{

    /** Logger객체. */
    private static Logger logger = Logger.getLoggerInstance(NoticeServiceBase.class.getName());

    /** SqlMapClient 객체. */
    protected static SqlMapClient sqlMap = SqlMapperWeb.getSqlMapWebInstance();

    public List selectNoticeList(int pageNo, int rowsPerPage, String domainId)
    {
    	return selectNoticeList(pageNo, rowsPerPage, domainId, null, null);
    }
    
    /**
     * 공지사항 리스트를 가져오는 메소드
     * @param notice
     * @return
     */
    public List selectNoticeList(int pageNo, int rowsPerPage, String domainId, String condition, String searchText)
    {
    	List list = null;
        try
        {
        	String currDate = DateUtil.getCurrDate("yyyyMMddHHmmss");
        	Notice obj = new Notice();
        	obj.setDomainId(domainId);
        	obj.setCurrDate(currDate);
        	obj.setStartNo((pageNo-1) * rowsPerPage + 1);
       	 	obj.setEndNo(pageNo * rowsPerPage);
       	 	obj.setCondition(condition);
       	 	obj.setRowsPerPage(rowsPerPage);
       	 	if (searchText != null)
       	 	{
	       	 	searchText = searchText.replaceAll("_", "\\\\_"); // 쿼리 escape지정
	       	 	searchText = searchText.replaceAll("%", "\\\\%"); // 쿼리 escape지정
       	 	}
       	 	obj.setSearchText(searchText);
            list = (List)sqlMap.queryForList("Notice.selectNoticeList", obj);
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return list;
    }
      
    /**
     * 공지사항 리스트 개수를 가져오는 메소드
     * @param pageNo
     * @param rowsPerPage
     * @param domainId
     * @return
     */
    public int selectNoticeListCount(String domainId, String condition, String searchText)
    {   	 
   	 	int iResult = 0;
   	 	String currDate = DateUtil.getCurrDate("yyyyMMddHHmmss");
   	 	Notice obj = new Notice();
   	 	obj.setDomainId(domainId);
   	 	obj.setCurrDate(currDate);
   	 	obj.setCondition(condition);
   	 	if (searchText != null)
   	 	{
	   	 	searchText = searchText.replaceAll("_", "\\\\_"); // 쿼리 escape지정
		 	searchText = searchText.replaceAll("%", "\\\\%"); // 쿼리 escape지정
   	 	}
	 	obj.setSearchText(searchText);
        try
        {
        	iResult = ((Integer)sqlMap.queryForObject("Notice.selectNoticeListCount", obj)).intValue();
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return iResult;
    } 
    
    /**
     * 공지사항에 대한 내용을 가져오는 메소드
     * @param notice
     * @return
     */
    public Notice selectNoticeByNoticeId(String noticeId)
    {
    	Notice noticeObj = null;
        try
        {
        	noticeObj = (Notice)sqlMap.queryForObject("Notice.selectNoticeByNoticeId", noticeId);
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return noticeObj;
    }
    
    /** 
     * 공지사항 읽음여부를 입력하는 메소드
     * @param accountId
     * @param noticeId
     * @param currDate
     * @return
     */
    public boolean insertNoticeReader(String accountId, String noticeId, String currDate)
    {
    	boolean bSuccess = false;
    	try
        {
    		NoticeReader noticeReaderObj = new NoticeReader();
    		noticeReaderObj.setAccountId(accountId);
    		noticeReaderObj.setNoticeId(noticeId);
    		noticeReaderObj.setCreatedDate(currDate);
        	sqlMap.insert("Notice.insertNoticeReader", noticeReaderObj);
        	bSuccess = true;
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return bSuccess;
    }
    
    /**
     * 공지사항 조회수를 업데이트 하는 메소드
     * @param noticeId
     * @return
     */
    public boolean updateNoticeHitCount(String noticeId)
    {
    	boolean bSuccess = false;
    	try
        {
        	sqlMap.update("Notice.updateNoticeHitCount", noticeId);
        	bSuccess = true;
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return bSuccess;
    }
    
    /**
     * 공지사항 다음글 가져오는 메소드
     * @param domainId
     * @param noticeId
     * @return
     */
    public Notice selectNextNotice(String domainId, String noticeId, String updatedDate)
    {
    	Notice noticeObj = null;
        try
        {
        	Notice tempObj = new Notice();
        	tempObj.setDomainId(domainId);
        	tempObj.setCurrDate(DateUtil.getCurrDate("yyyyMMddHHmmss"));
        	tempObj.setUpdatedDate(updatedDate);
        	tempObj.setNoticeId(noticeId);
        	noticeObj = (Notice)sqlMap.queryForObject("Notice.selectNextNotice", tempObj);
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return noticeObj;
    }
    
    /**
     * 공지사항 이전글 가져오는 메소드
     * @param domainId
     * @param noticeId
     * @return
     */
    public Notice selectPrevNotice(String domainId, String noticeId, String updatedDate)
    {
    	Notice noticeObj = null;
        try
        {
        	Notice tempObj = new Notice();
        	tempObj.setDomainId(domainId);
        	tempObj.setCurrDate(DateUtil.getCurrDate("yyyyMMddHHmmss"));
        	tempObj.setUpdatedDate(updatedDate);
        	tempObj.setNoticeId(noticeId);
        	noticeObj = (Notice)sqlMap.queryForObject("Notice.selectPrevNotice", tempObj);
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return noticeObj;
    }
    
    
}
